﻿using Microsoft.AspNetCore.Mvc;
using NewLife.Caching;

namespace newlife.cache.api.Controllers
{
    /// <summary>
    /// 内存缓存操作
    /// </summary>
    [Route("api/[controller]/[action]")]
    [ApiController]
    public class MemoryCacheController : ControllerBase
    {
        [HttpPost]
        public IActionResult Set(string key, string value)
        {
            return Ok(Cache.Default.Set(key, value, 60)); // 60 秒过期
        }

        [HttpGet]
        public IActionResult Get(string key)
        { 
            return Ok(Cache.Default.Get<string>(key));
        }

        [HttpGet]
        public IActionResult Expire(string key)
        {
            return Ok(Cache.Default.GetExpire(key).TotalSeconds);
        }

        [HttpGet]
        public IActionResult Exists(string key)
        {
            return Ok(Cache.Default.ContainsKey(key));
        }

        [HttpGet]
        public IActionResult Count()
        {
            return Ok(Cache.Default.Count);
        }

        [HttpGet]
        public IActionResult Keys()
        {
            return Ok(Cache.Default.Keys);
        }

        [HttpDelete]
        public IActionResult Remove(string key)
        {
            return Ok(Cache.Default.Remove(key));
        }

        [HttpDelete]
        public IActionResult RemoveAll()
        {
            Cache.Default.Clear();
            return NoContent();
        }
    }
}
